<template>
  <div class="app-container">
    <el-form class="multiple-from" :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item prop="plotId" label="小区名称：">
        <el-select v-model="queryParams.plotId" placeholder="请选择小区名称" clearable style="width: 170px"
          @change="xiaoquChange()">
          <el-option v-for="dict in xiaoquData" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="listFlag >= 1" prop="buildId" label="所属楼栋：">
        <el-select v-model="queryParams.buildId" placeholder="请选择楼栋" clearable style="width: 170px"
          @change="loudonChange()">
          <el-option v-for="dict in loudongData" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="listFlag >= 2" prop="unitId" label="所属单元：">
        <el-select v-model="queryParams.unitId" placeholder="请选择单元" clearable style="width: 170px">
          <el-option v-for="dict in danyuanData" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="listFlag == 3" label="住户分类：" prop="type">
        <el-select v-model="queryParams.houseHoldType" placeholder="请选择住户分类" clearable>
          <el-option v-for="(item, index) in userType" :key="index" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="设备名称：" prop="equipmentName">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称"></el-input>
      </el-form-item>
      <el-form-item label="设备编号：" prop="equipmentCode">
        <el-input v-model="queryParams.equipmentCode" placeholder="请输入设备编号"></el-input>
      </el-form-item> -->

      <!-- <el-form-item prop="typeValue" label="当前模型:" v-if="listFlag === 0" style="margin-left: -8px;">
        <el-select v-model="queryParams.typeValue" placeholder="请选择" clearable style="width: 170px">
          <el-option v-for="dict in typeData" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div style="margin-bottom: 5px;display: flex;justify-content: space-between;">
      <div>
        <el-button :class="listFlag === 3 ? 'active' : 'unactive'" @click="checkList(3)" type="primary">住户列表</el-button>
        <el-button :class="listFlag === 0 ? 'active' : 'unactive'" @click="checkList(0)" type="primary">小区列表</el-button>
        <el-button :class="listFlag === 1 ? 'active' : 'unactive'" @click="checkList(1)" type="primary">楼栋列表</el-button>
        <el-button :class="listFlag === 2 ? 'active' : 'unactive'" @click="checkList(2)" type="primary">单元列表</el-button>
      </div>
      <div>
        <el-button type="primary" icon="el-icon-s-operation" @click="openYiJian">一键调控</el-button>
        <el-button type="primary" icon="el-icon-s-operation" @click="handelYiJian">批量调控</el-button>
        <el-button v-if="listFlag != 3" type="primary" icon="el-icon-document-checked"
          @click="handelClickPiLiang">批量存档</el-button>
        <el-button type="primary" size="mini" icon="el-icon-search" @click="showSearch = !showSearch"></el-button>
        <el-button type="primary" size="mini" icon="el-icon-refresh-right" @click="getList"></el-button>
      </div>
    </div>

    <el-table ref="balanceTable" v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
      <el-table-column key="1" v-if="selectFlag" type="selection" width="55" align="center" />
      <el-table-column key="2" label="序号" align="center" type="index"></el-table-column>
      <el-table-column key="3" show-overflow-tooltip label="小区名称" align="center" prop="plotName" />
      <el-table-column key="4" v-if="listFlag !== 0" show-overflow-tooltip label="楼栋" align="center"
        prop="buildName"></el-table-column>
      <el-table-column key="5" v-if="listFlag === 2 || listFlag === 3" show-overflow-tooltip label="单元" align="center"
        prop="unitName"></el-table-column>
      <el-table-column key="6" v-if="listFlag === 0" show-overflow-tooltip label="楼栋数量" align="center" prop="buildNum" />
      <el-table-column key="7" v-if="listFlag === 0 || listFlag === 1" show-overflow-tooltip label="单元数量" align="center"
        prop="unitNum" />
      <el-table-column v-if="listFlag === 3" show-overflow-tooltip label="住户分类" align="center" prop="classifyName" />
      <el-table-column key="8" show-overflow-tooltip label="设备数量" align="center" prop="equipmentNum" />
      <el-table-column key="9" show-overflow-tooltip label="设备正常数量" align="center" prop="normalNum" />
      <el-table-column key="10" show-overflow-tooltip label="设备异常数量" align="center" prop="abnormalNum" />

      <el-table-column key="11" v-if="listFlag === 3" show-overflow-tooltip label="楼层" align="center"
        prop="floor"></el-table-column>
      <el-table-column key="12" v-if="listFlag === 3" show-overflow-tooltip label="户号" align="center" prop="houseNumber">
        <template slot-scope="scope">
          <div @click="goDataList(scope.row)" style="cursor: pointer;color: rgb(64, 158, 255);text-align: center;">{{
            scope.row.houseNumber }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column key="13" v-if="listFlag === 0" show-overflow-tooltip label="设备名称" align="center"
        prop="equipmentName" /> -->
      <!-- <el-table-column key="14" v-if="listFlag === 0" show-overflow-tooltip label="设备编号" align="center"
        prop="equipmentCode"></el-table-column> -->
      <!-- <el-table-column key="15" v-if="listFlag === 0" show-overflow-tooltip label="阀门位置" align="center"
        prop="postion"></el-table-column> -->
      <el-table-column key="16" v-if="listFlag === 0" show-overflow-tooltip label="调控方法" align="center"
        prop="regulationMethod"></el-table-column>
      <el-table-column key="17" show-overflow-tooltip label="供水温度" align="center"
        prop="waterSupplyTemperature"></el-table-column>
      <el-table-column key="18" show-overflow-tooltip label="回水温度" align="center"
        prop="returnWaterTemperature"></el-table-column>

      <el-table-column key="19" show-overflow-tooltip label="开度" align="center" prop="valveOpen"></el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleRegulationOne(scope.row)">调控</el-button>
          <el-button v-if="listFlag != 3" size="mini" type="text" @click="handleCunDang(scope.row)">存档</el-button>
          <el-button size="mini" type="text" @click="handleLock(scope.row, scope.$index)">{{ scope.row.lockStatus == 1 ?
            '解锁' :
            '锁定' }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!--调控弹窗 -->
    <el-dialog :title="regulationType == 1 ? '调控' : '批量调控'" :visible.sync="dialogVisible" width="85%" append-to-body>
      <div class="dialog-warp">
        <el-form :model="regulationData" :rules="rules" ref="chaobiaoForm" label-width="160px" :inline="true"
          label-position="left">
          <el-form-item prop="regulationMethod" label="调控方法">
            <el-select v-model="regulationData.regulationMethod" placeholder="请选择调控方法" clearable>
              <el-option v-for="dict in tiaokongData" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>

          <el-form-item v-if="listFlag <= 1" prop="buildingValveOpen" label="楼栋阀门默认值">
            <el-input v-model="regulationData.buildingValveOpen" placeholder="楼栋阀门默认值(0-100)"></el-input>
          </el-form-item>

          <el-form-item v-if="listFlag <= 2" prop="unitValveOpen" label="单元阀门默认值">
            <el-input v-model="regulationData.unitValveOpen" placeholder="单元阀门默认值(0-100)"></el-input>
          </el-form-item>

          <el-form-item v-if="listFlag != 2" prop="householdValveOpen" label="住户阀门默认值">
            <el-input v-model="regulationData.householdValveOpen" placeholder="住户阀门默认值(0-100)"></el-input>
          </el-form-item>

          <el-form-item v-if="listFlag == 1" label="优先级" prop="priority">
            <el-select v-model="regulationData.priority" placeholder="请选择优先级" clearable>
              <el-option v-for="dict in prioritys" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="阀门调整步进值" prop="stepValue">
            <el-input v-model="regulationData.stepValue" placeholder="阀门调整步进值"></el-input>
          </el-form-item> -->

          <el-form-item v-if="regulationData.regulationMethod == 2" label="调控策略" prop="controlStrategy">
            <el-select v-model="regulationData.controlStrategy" placeholder="请选择调控策略" clearable>
              <el-option v-for="dict in tiaokongcelueFilter" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>

          <div v-if="regulationData.controlStrategy == 1 && regulationData.regulationMethod == 2">
            <el-form-item prop="returnWaterTemperature" label="回水温度目标值">
              <el-input v-model="regulationData.returnWaterTemperature" placeholder="回水温度目标值"></el-input>
            </el-form-item>
            <el-form-item label="回水温度波动值" prop="returnWaterTemperatureFluctuating">
              <el-input type="number" v-model="regulationData.returnWaterTemperatureFluctuating"
                placeholder="回水温度波动值"></el-input>
            </el-form-item>
            <el-form-item label="回水温度波动范围">
              <el-input :value="returnWaterTemperatureRange" disabled type="text" placeholder="回水温度波动范围"></el-input>
            </el-form-item>
            <!-- <el-form-item label="回水温度异常最小值" prop="returnWaterMinValue">
              <el-input v-model="regulationData.returnWaterMinValue" type="number" placeholder="回水温度异常最小值"></el-input>
            </el-form-item>
            <el-form-item label="回水温度异常最大值" prop="returnWaterMaxValue">
              <el-input v-model="regulationData.returnWaterMaxValue" type="number" placeholder="回水温度异常最大值"></el-input>
            </el-form-item> -->
          </div>

          <div v-else-if="regulationData.controlStrategy == 2 && regulationData.regulationMethod == 2">
            <el-form-item prop="targetRoomTemperature" label="室内温度目标值">
              <el-input v-model="regulationData.targetRoomTemperature" placeholder="室内温度目标值"></el-input>
            </el-form-item>
            <el-form-item label="室内温度波动值" prop="targetRoomTemperatureFluctuating">
              <el-input type="number" v-model="regulationData.targetRoomTemperatureFluctuating"
                placeholder="室内温度波动值"></el-input>
            </el-form-item>
            <el-form-item label="室内温度波动范围">
              <el-input :value="targetRoomRange" disabled type="text" placeholder="室内温度波动范围"></el-input>
            </el-form-item>
            <!-- <el-form-item label="室内温度异常最小值" prop="targetRoomMinValueP">
              <el-input v-model="regulationData.targetRoomMinValueP" type="number" placeholder="室内温度异常最小值"></el-input>
            </el-form-item>
            <el-form-item label="室内温度异常最大值" prop="targetRoomMaxValue">
              <el-input v-model="regulationData.targetRoomMaxValue" type="number" placeholder="室内温度异常最大值"></el-input>
            </el-form-item> -->
          </div>

          <div v-else-if="regulationData.controlStrategy == 3 && regulationData.regulationMethod == 2">
            <el-form-item label="供回水平均温度目标值" prop="farReturnTemperature">
              <el-input type="number" v-model="regulationData.farReturnTemperature"
                placeholder="请输入供回水平均温度目标值"></el-input>
            </el-form-item>
            <el-form-item label="供回水平均温度波动值" prop="farReturnTemperatureFluctuating">
              <el-input type="number" v-model="regulationData.farReturnTemperatureFluctuating"
                placeholder="供回水平均温度波动值"></el-input>
            </el-form-item>
            <el-form-item label="供回水平均温度波动范围" prop="farReturnTemperatureFluctuating">
              <el-input disabled :value="farReturnTemperatureRange" type="text" placeholder="供回水平均温度波动范围"></el-input>
            </el-form-item>
            <!-- <el-form-item label="供回水平均温度异常最小值" prop="farReturnMinValue">
              <el-input v-model="regulationData.farReturnMinValue" type="number" placeholder="供回水平均温度异常最小值"></el-input>
            </el-form-item>
            <el-form-item label="供回水平均温度异常最大值" prop="farReturnMaxValue">
              <el-input v-model="regulationData.farReturnMaxValue" type="number" placeholder="供回水平均温度异常最大值"></el-input>
            </el-form-item> -->
          </div>

          <div v-else-if="regulationData.controlStrategy == 4 && regulationData.regulationMethod == 2">
            <el-form-item prop="velocityFlow" label="流量目标值">
              <el-input v-model="regulationData.velocityFlow" placeholder="流量目标值"></el-input>
            </el-form-item>
            <el-form-item label="流量目标波动值" prop="velocityFlowFluctuating">
              <el-input type="number" v-model="regulationData.velocityFlowFluctuating" placeholder="流量波动值"></el-input>
            </el-form-item>
            <el-form-item label="流量波动范围">
              <el-input disabled :value="velocityFlowRange" type="text" placeholder="流量波动范围"></el-input>
            </el-form-item>
            <!-- <el-form-item label="流量目标异常最小值" prop="velocityFlowMinValue">
              <el-input v-model="regulationData.velocityFlowMinValue" type="number" placeholder="流量目标异常最小值"></el-input>
            </el-form-item>
            <el-form-item label="流量目标异常最大值" prop="velocityFlowMaxValue">
              <el-input v-model="regulationData.velocityFlowMaxValue" type="number" placeholder="流量目标异常最大值"></el-input>
            </el-form-item> -->
          </div>

          <div v-else-if="regulationData.controlStrategy == 5 && regulationData.regulationMethod == 2">
            <el-form-item prop="heat" label="热量目标值">
              <el-input v-model="regulationData.heat" placeholder="热量目标值"></el-input>
            </el-form-item>
            <el-form-item label="热量波动值" prop="heatFluctuating">
              <el-input type="number" v-model="regulationData.heatFluctuating" placeholder="热量波动值"></el-input>
            </el-form-item>
            <el-form-item label="热量目标波动范围">
              <el-input :value="heatRange" disabled type="text" placeholder="热量目标波动范围"></el-input>
            </el-form-item>
            <!-- <el-form-item label="热量目标异常最小值" prop="heatMinValue">
              <el-input v-model="regulationData.heatMinValue" type="number" placeholder="热量目标异常最小值"></el-input>
            </el-form-item>
            <el-form-item label="热量目标异常最大值" prop="heatMaxValue">
              <el-input v-model="regulationData.heatMaxValue" type="number" placeholder="热量目标异常最大值"></el-input>
            </el-form-item> -->
          </div>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancellation">取 消</el-button>
        <el-button type="primary" @click="handelClickOk">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 一键调控 -->
    <el-dialog title="一键调控" :visible.sync="yijiandialogVisible" width="85%" append-to-body>
      <div class="dialog-warp">
        <el-form :model="regulationData" :rules="rules" ref="chaobiaoyijianForm" label-width="160px" :inline="true"
          label-position="left">
          <el-form-item prop="regulationMethod" label="调控方法">
            <el-select v-model="regulationData.regulationMethod" placeholder="请选择调控方法" clearable>
              <el-option v-for="dict in tiaokongData" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item v-if="listFlag == 0 || listFlag == 1" prop="buildingValveOpen" label="楼栋阀门默认值">
            <el-input v-model="regulationData.buildingValveOpen" placeholder="楼栋阀门默认值(0-100)"></el-input>
          </el-form-item>
          <el-form-item v-if="listFlag <= 2" prop="unitValveOpen" label="单元阀门默认值">
            <el-input v-model="regulationData.unitValveOpen" placeholder="单元阀门默认值(0-100)"></el-input>
          </el-form-item>
          <el-form-item prop="householdValveOpen" label="住户阀门默认值">
            <el-input v-model="regulationData.householdValveOpen" placeholder="住户阀门默认值(0-100)"></el-input>
          </el-form-item>
          <el-form-item v-if="listFlag == 1" label="优先级" prop="priority">
            <el-select v-model="regulationData.priority" placeholder="请选择优先级" clearable>
              <el-option v-for="dict in prioritys" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item v-if="regulationData.regulationMethod == 2" label="调控策略" prop="controlStrategy">
            <el-select v-model="regulationData.controlStrategy" placeholder="请选择调控策略" clearable>
              <el-option v-for="dict in tiaokongcelueFilter" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <div v-if="regulationData.controlStrategy == 1 && regulationData.regulationMethod == 2">
            <el-form-item prop="returnWaterTemperature" label="回水温度目标值">
              <el-input v-model="regulationData.returnWaterTemperature" placeholder="回水温度目标值"></el-input>
            </el-form-item>
            <el-form-item label="回水温度波动值" prop="returnWaterTemperatureFluctuating">
              <el-input type="number" v-model="regulationData.returnWaterTemperatureFluctuating"
                placeholder="回水温度波动值"></el-input>
            </el-form-item>
            <el-form-item label="回水温度波动范围">
              <el-input :value="returnWaterTemperatureRange" disabled type="text" placeholder="回水温度波动范围"></el-input>
            </el-form-item>
          </div>
          <div v-else-if="regulationData.controlStrategy == 2 && regulationData.regulationMethod == 2">
            <el-form-item prop="targetRoomTemperature" label="室内温度目标值">
              <el-input v-model="regulationData.targetRoomTemperature" placeholder="室内温度目标值"></el-input>
            </el-form-item>
            <el-form-item label="室内温度波动值" prop="targetRoomTemperatureFluctuating">
              <el-input type="number" v-model="regulationData.targetRoomTemperatureFluctuating"
                placeholder="室内温度波动值"></el-input>
            </el-form-item>
            <el-form-item label="室内温度波动范围">
              <el-input :value="targetRoomRange" disabled type="text" placeholder="室内温度波动范围"></el-input>
            </el-form-item>
          </div>
          <div v-else-if="regulationData.controlStrategy == 3 && regulationData.regulationMethod == 2">
            <el-form-item label="供回水平均温度目标值" prop="farReturnTemperature">
              <el-input type="number" v-model="regulationData.farReturnTemperature"
                placeholder="请输入供回水平均温度目标值"></el-input>
            </el-form-item>
            <el-form-item label="供回水平均温度波动值" prop="farReturnTemperatureFluctuating">
              <el-input type="number" v-model="regulationData.farReturnTemperatureFluctuating"
                placeholder="供回水平均温度波动值"></el-input>
            </el-form-item>
            <el-form-item label="供回水平均温度波动范围" prop="farReturnTemperatureFluctuating">
              <el-input disabled :value="farReturnTemperatureRange" type="text" placeholder="供回水平均温度波动范围"></el-input>
            </el-form-item>
          </div>
          <div v-else-if="regulationData.controlStrategy == 4 && regulationData.regulationMethod == 2">
            <el-form-item prop="velocityFlow" label="流量目标值">
              <el-input v-model="regulationData.velocityFlow" placeholder="流量目标值"></el-input>
            </el-form-item>
            <el-form-item label="流量目标波动值" prop="velocityFlowFluctuating">
              <el-input type="number" v-model="regulationData.velocityFlowFluctuating" placeholder="流量波动值"></el-input>
            </el-form-item>
            <el-form-item label="流量波动范围">
              <el-input disabled :value="velocityFlowRange" type="text" placeholder="流量波动范围"></el-input>
            </el-form-item>
          </div>
          <div v-else-if="regulationData.controlStrategy == 5 && regulationData.regulationMethod == 2">
            <el-form-item prop="heat" label="热量目标值">
              <el-input v-model="regulationData.heat" placeholder="热量目标值"></el-input>
            </el-form-item>
            <el-form-item label="热量波动值" prop="heatFluctuating">
              <el-input type="number" v-model="regulationData.heatFluctuating" placeholder="热量波动值"></el-input>
            </el-form-item>
            <el-form-item label="热量目标波动范围">
              <el-input :value="heatRange" disabled type="text" placeholder="热量目标波动范围"></el-input>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelYiJian">取 消</el-button>
        <el-button type="primary" @click="oneRegulation">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="存档" :visible.sync="cunDiaLog" width="500px" append-to-body>
      <el-form :model="cunForm" ref="cunForm" inline label-width="80px" :rules="cunRules">
        <div class="textarea-warp">
          <el-form-item label="存档备注" prop="cunValue">
            <el-input type="textarea" v-model="cunForm.cunValue" placeholder="请输入存档备注"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelCun">取消</el-button>
        <el-button type="primary" @click="handelClickCun">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 锁定 -->
    <el-dialog :title="lockData.lockStatus == 0 ? '锁定' : '解锁'" :visible.sync="lockDialog" width="30%">
      <span style="color: #ffffff;">{{ lockData.lockStatus == 0 ? '是否将当前所选数据进行锁定，锁定后，自动调控将不生效' : '是否将当前所选数据进行解锁' }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="canceLock">取 消</el-button>
        <el-button type="primary" @click="confirmLock">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType, refreshCache } from "@/api/system/dict/type";
import { listHousehold, listPlot, listBuild, listBuildUnit, getDataSetInfo, addOrUpdateControlSet, batchControlSet, lockOrUnlock,oneKeyRegulation } from "@/api/monitoring/balance"
import { addArchivesList } from "@/api/equipment/archives"
import { listCPlotSelect, listBuildSelect, listBuildUnitSelect, listHouseholdClassifySelect } from "@/api/system/utils"

export default {
  name: "Dict",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      tiaokongCelue: [
        {
          label: "回水温度",
          value: '1',
          ruleNames: [
            "returnWaterTemperature",
            "returnWaterTemperatureFluctuating",
            "returnWaterMinValue",
            "returnWaterMaxValue",
          ],
        },
        {
          label: "室内温度",
          value: '2',
          ruleNames: [
            "targetRoomTemperature",
            "targetRoomTemperatureFluctuating",
            "targetRoomMinValueP",
            "targetRoomMaxValue",
          ],
        },
        {
          label: "供回水平均温度",
          value: '3',
          ruleNames: [
            "farReturnTemperature",
            "farReturnTemperatureFluctuating",
            "farReturnMinValue",
            "farReturnMaxValue",
          ],
        },
        {
          label: "流量",
          value: '4',
          ruleNames: [
            "velocityFlow",
            "velocityFlowFluctuating",
            "velocityFlowMinValue",
            "velocityFlowMaxValue",
          ],
        },
        {
          label: "热量",
          value: '5',
          ruleNames: [
            "heat",
            "heatFluctuating",
            "heatMinValue",
            "heatMaxValue",
          ],
        },
      ],
      tiaokongData: [
        { label: '手动调控', value: '1' },
        { label: '自动调控', value: '2' },
      ],
      tiaozhengData: [
        { label: '步进值调整', value: 0 },
        { label: '直接调整', value: 1 },
      ],
      prioritys: [
        { label: "楼栋", value: 1 },
        { label: "单元", value: 2 },
      ],
      selectFlag: false,
      // 当前列表 0:小区 1：楼栋 2：单元 3：住户
      listFlag: 3,
      // 存档
      cunDiaLog: false,
      cunForm: {
        cunValue: null,
        archiveSave: []
      },
      cunRules: {
        cunValue: [
          { required: true, message: '请输入存档备注', trigger: 'blur' },
        ],
      },
      // 抄表
      dialogVisible: false,
      //一键调控
      yijiandialogVisible: false,
      //锁定显示
      lockDialog: false,
      lockData: {},
      index: -1,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区名字
      xiaoquData: [],
      loudongData: [],
      danyuanData: [],
      // 字典表格数据
      typeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildId: null,
        unitId: null,
        plotId: null,
        typeValue: null,
        equipmentName: null,
        equipmentCode: null,
        houseHoldType: null,
      },
      // 调控数据
      regulationData: {
        id: null,
        // 类型 0:小区 1：楼栋 2：单元 3：住户
        type: 0,
        // 优先级（1：楼栋 2：单元）
        priority: null,
        // 热量目标
        heat: null,
        heatFluctuating: null,
        heatMinValue: null,
        heatMaxValue: null,
        // 室内温度目标值
        targetRoomTemperature: null,
        targetRoomTemperatureFluctuating: null,
        targetRoomMinValueP: null,
        targetRoomMaxValue: null,
        // 回水温度目标
        returnWaterTemperature: null,
        returnWaterTemperatureFluctuating: null,
        returnWaterMinValue: null,
        returnWaterMaxValue: null,
        // 小区id
        plotId: null,
        // 楼栋id
        buildId: null,
        // 单元id
        unitId: null,
        // 调控方法（1：手动调控 2：自动调控）
        regulationMethod: null,
        // 调控策略
        controlStrategy: null,
        // 楼栋阀门默认开度
        buildingValveOpen: null,
        // 单元阀门默认开度
        unitValveOpen: null,
        // 住户阀门默认开度
        householdValveOpen: null,
        // 阀门调整步进值
        stepValue: null,
        // 供回水平均温度
        farReturnTemperature: null,
        farReturnTemperatureFluctuating: null,
        farReturnMinValue: null,
        farReturnMaxValue: null,
        // 流量目标值
        velocityFlow: null,
        velocityFlowFluctuating: null,
        velocityFlowMinValue: null,
        velocityFlowMaxValue: null,
      },
      // 调控方式(2:一键调控  1:单个调控)
      regulationType: 1,
      rules: {
        regulationMethod: [
          { required: true, message: "请选择调控方法", trigger: "blur" },
        ],
        controlStrategy: [
          { required: true, message: "请选择调控策略", trigger: "blur" },
        ],
        buildingValveOpen: [
          { required: true, message: "请选择楼栋阀门默认值", trigger: "blur" },
        ],
        stepValue: [
          { required: true, message: "请输入阀门调整步进值", trigger: "blur" },
        ],
        unitValveOpen: [
          { required: true, message: "请选择单元阀门默认值", trigger: "blur" },
        ],
        householdValveOpen: [
          { required: true, message: "请选择住户阀门默认值", trigger: "blur" },
        ],
        returnWaterTemperature: [
          { required: false, message: "请输入回水温度目标值", trigger: "blur" },
        ],
        priority: [
          { required: true, message: "请选择优先级", trigger: "blur" },
        ],
        returnWaterTemperatureFluctuating: [
          {
            required: false,
            message: "请输入回水温度目标波动值",
            trigger: "blur",
          },
        ],
        returnWaterMinValue: [
          {
            required: false,
            message: "请输入回水温度异常最小值",
            trigger: "blur",
          },
        ],
        returnWaterMaxValue: [
          {
            required: false,
            message: "请输入回水温度异常最大值",
            trigger: "blur",
          },
        ],
        farReturnTemperature: [
          {
            required: false,
            message: "请输入供回水平均温度目标值",
            trigger: "blur",
          },
        ],
        farReturnTemperatureFluctuating: [
          {
            required: false,
            message: "请输入供回水平均温度波动值",
            trigger: "blur",
          },
        ],
        farReturnMinValue: [
          {
            required: false,
            message: "请输入供回水平均温度异常最小值",
            trigger: "blur",
          },
        ],
        farReturnMaxValue: [
          {
            required: false,
            message: "请输入供回水平均温度异常最大值",
            trigger: "blur",
          },
        ],
        targetRoomTemperature: [
          { required: false, message: "请输入室内温度目标值", trigger: "blur" },
        ],
        targetRoomTemperatureFluctuating: [
          {
            required: false,
            message: "请输入室内温度目标波动值",
            trigger: "blur",
          },
        ],
        targetRoomMinValueP: [
          {
            required: false,
            message: "请输入室内温度异常最小值",
            trigger: "blur",
          },
        ],
        targetRoomMaxValue: [
          {
            required: false,
            message: "请输入室内温度异常最大值",
            trigger: "blur",
          },
        ],
        velocityFlow: [
          { required: false, message: "请输入流量目标值", trigger: "blur" },
        ],
        velocityFlowFluctuating: [
          { required: false, message: "请输入流量目标波动值", trigger: "blur" },
        ],
        velocityFlowMaxValue: [
          {
            required: false,
            message: "请输入流量目标异常最大值",
            trigger: "blur",
          },
        ],
        velocityFlowMinValue: [
          {
            required: false,
            message: "请输入流量目标异常最小值",
            trigger: "blur",
          },
        ],
        heat: [
          { required: false, message: "请输入热量目标值", trigger: "blur" },
        ],
        heatFluctuating: [
          { required: false, message: "请输入热量目标波动值", trigger: "blur" },
        ],
        heatMinValue: [
          {
            required: false,
            message: "请输入热量目标异常最小值",
            trigger: "blur",
          },
        ],
        heatMaxValue: [
          {
            required: false,
            message: "请输入热量目标异常最大值",
            trigger: "blur",
          },
        ],
      },
      ids: [],
      archives: [],
      // 住户分类
      userType: [],
    };
  },
  created() {
    
    listCPlotSelect({}).then(res => {
      this.xiaoquData = res.rows
      this.queryParams.plotId = res.rows[0].value
    }).then(res=>{
      listBuildSelect({ plotId:this.queryParams.plotId}).then((res) => {
          this.loudongData = res.rows;
        });
      this.getList();
    });
    // listCustomModelSelect({}).then(res => {
    //   this.typeData = res.rows
    // });
  },
  watch: {
    "regulationData.controlStrategy": {
      handler(n, o) {
        if (n) {
          let val1 = this.tiaokongCelue.find((item) => item.value == n);
          let val2 = this.tiaokongCelue.find((item) => item.value == o);
          for (const key in this.rules) {
            if (val1 && val1.ruleNames.includes(key)) {
              this.rules[key].forEach((element) => {
                element.required = true;
              });
            } else if (val2 && val2.ruleNames.includes(key)) {
              this.rules[key].forEach((element) => {
                element.required = false;
              });
            }
          }
        }
      },
      deep: true,
    },
  },
  computed: {
    heatRange() {
      let number = Number(this.regulationData.heat);
      let fluctua = Number(this.regulationData.heatFluctuating);
      if (number && fluctua) {
        return number - fluctua + "-" + (number + fluctua);
      } else {
        return "";
      }
    },
    targetRoomRange() {
      let number = Number(this.regulationData.targetRoomTemperature);
      let fluctua = Number(this.regulationData.targetRoomTemperatureFluctuating);
      if (number && fluctua) {
        return number - fluctua + "-" + (number + fluctua);
      } else {
        return "";
      }
    },
    velocityFlowRange() {
      let number = Number(this.regulationData.velocityFlow);
      let fluctua = Number(this.regulationData.velocityFlowFluctuating);
      if (number && fluctua) {
        return number - fluctua + "-" + (number + fluctua);
      } else {
        return "";
      }
    },
    farReturnTemperatureRange() {
      let number = Number(this.regulationData.farReturnTemperature);
      let fluctua = Number(this.regulationData.farReturnTemperatureFluctuating);
      if (number && fluctua) {
        return number - fluctua + "-" + (number + fluctua);
      } else {
        return "";
      }
    },
    returnWaterTemperatureRange() {
      let number = Number(this.regulationData.returnWaterTemperature);
      let fluctua = Number(this.regulationData.returnWaterTemperatureFluctuating);
      if (number && fluctua) {
        return number - fluctua + "-" + (number + fluctua);
      } else {
        return "";
      }
    },
    tiaokongcelueFilter() {
      return this.tiaokongCelue
      // if (this.regulationData.type == 3) {
      //   return this.tiaokongCelue
      // } else {
      //   return this.tiaokongCelue.filter(item => {
      //     return item.value != 2
      //   })
      // }
    }
  },
  methods: {
    // 获取平衡调控设置数据
    getDataSetInfoFun(data) {
      getDataSetInfo(data).then(res => {
        this.dialogVisible = true
        if (res) {
          for (const key in res) {
            this.regulationData[key] = res[key]
          }
        }
        for (const key in data) {
          this.regulationData[key] = data[key]
        }
      }, err => {
        this.$message.error('获取数据失败')
      })
    },
    xiaoquChange() {
      this.loudongData = []
      this.danyuanData = []
      listBuildSelect({ plotId: this.queryParams.plotId }).then(res => {
        this.queryParams.buildId = null;
        this.queryParams.unitId = null;
        this.loudongData = res.rows
      });
      listHouseholdClassifySelect({ plotId: this.queryParams.plotId }).then(res => {
        this.userType = res.rows
      });
    },
    loudonChange() {
      this.danyuanData = []
      listBuildUnitSelect({ buildId: this.queryParams.buildId }).then(res => {
        this.queryParams.unitId = null;
        this.danyuanData = res.rows
      });
    },
    // 切换列表
    checkList(num) {
      this.listFlag = num
      this.getList()
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      if (this.listFlag === 3) {
        listHousehold(this.queryParams).then(res => {
          this.typeList = res.rows;
          this.total = res.total
          this.loading = false;
        });
      } else if (this.listFlag === 0) {
        listPlot(this.queryParams).then(res => {
          this.typeList = res.rows;
          this.total = res.total
          this.loading = false;
        });
      } else if (this.listFlag === 1) {
        listBuild(this.queryParams).then(res => {
          this.typeList = res.rows;
          this.total = res.total
          this.loading = false;
        });
      } else if (this.listFlag === 2) {
        listBuildUnit(this.queryParams).then(res => {
          this.typeList = res.rows;
          this.total = res.total
          this.loading = false;
        });
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.archives = selection.map(item => {
        return { type: this.listFlag, plotId: item.plotId, buildId: item.buildId, unitId: item.unitId, valveOpen: item.valveOpen }
      })
      this.ids = selection.map(item => {
        return { type: this.listFlag, plotId: item.plotId, buildId: item.buildId, unitId: item.unitId, householdId: item.householdId }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.loudongData = []
      this.danyuanData = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 一键调控模态框
      openYiJian() {
        this.yijiandialogVisible = true
        this.resetForm('chaobiaoyijianForm')
    },
    //取消一键调控模态框
    cancelYiJian() {
      this.yijiandialogVisible = false
      this.resetForm('chaobiaoyijianForm')
    },
    //一键调控
    oneRegulation(){
      let type = this.listFlag.toString()
      let {buildId,unitId,plotId,houseHoldType} = this.queryParams
      let regulationParams = {...this.regulationData,buildId,unitId,plotId,houseHoldType,type}
      console.log("一键调控全部参数",regulationParams);
      this.$refs['chaobiaoyijianForm'].validate((valid) =>{
        if(valid){
          oneKeyRegulation(regulationParams).then(res=>{
            this.$message.success('操作成功')
            this.yijiandialogVisible = false
            this.resetForm('chaobiaoyijianForm')
          })
        }
      })
      // this.$modal
      //     .confirm('是否一键调控')
      //     .then(()=>{
      //       return oneKeyRegulation(regulationParams)
      //     })
      //     .then(() => {
      //       this.$modal.msgSuccess("一键调控成功");
      //     })
      //     .catch(() => { });
      
      
    },
    // 批量调控
    handelYiJian() {
      if (this.ids.length === 0) {
        this.selectFlag = !this.selectFlag
      } else {
        this.regulationType = 2
        this.dialogVisible = true
        this.resetForm('chaobiaoForm')
      }
    },
    // 批量存储
    handelClickPiLiang() {
      if (this.archives.length === 0) {
        this.selectFlag = !this.selectFlag
      } else {
        this.resetForm('cunForm')
        this.cunDiaLog = true
        this.cunForm.archiveSave = this.archives
      }
    },
    /** 单个调控 */
    handleRegulationOne(row) {
      this.regulationType = 1
      // this.resetForm('chaobiaoForm')
      let objData = {
        plotId: row.plotId,
        buildId: row.buildId,
        unitId: row.unitId,
        householdId: row.householdId,
        type: this.listFlag,
      }
      for (const key in objData) {
        this.regulationData[key] = objData[key]
      }
      this.getDataSetInfoFun(objData)
    },
    // 取消调控
    cancellation() {
      this.dialogVisible = false
      this.resetForm('chaobiaoForm')
    },
    /** 存档 */
    handleCunDang(row) {
      if (row.valveOpen == null) {
        this.$message({ message: '开度不能为空', type: 'warning' })
        return;
      }
      this.cunForm.archiveSave = [{ type: this.listFlag, plotId: row.plotId, buildId: row.buildId, unitId: row.unitId, valveOpen: row.valveOpen }];
      this.cunDiaLog = true
    },
    // 锁定
    handleLock(row, i) {
      this.index = i
      this.lockData = row
      this.lockDialog = true
    },
    // 确认锁定
    confirmLock() {
      let lockData = this.lockData
      let id = lockData.plotId
      if (this.listFlag == 1) {
        id = lockData.buildId
      } else if (this.listFlag == 2) {
        id = lockData.unitId
      } else if (this.listFlag == 3) {
        id = lockData.householdId
      }
      let objData = {
        id,
        lockStatus: lockData.lockStatus == 0 ? 1 : 0,
        type: this.listFlag,
      }
      lockOrUnlock(objData).then(res => {
        let msg = objData.lockStatus == 0 ? '解锁成功' : '锁定成功'
        this.$message.success(msg)

        this.lockData = {}
        let i = this.index
        this.typeList[i].lockStatus = objData.lockStatus
        this.index = -1
        this.lockDialog = false
      })
    },
    canceLock() {
      this.lockData = {}
      this.index = -1
      this.lockDialog = false
    },
    // 存档确定
    handelClickCun() {
      this.$refs['cunForm'].validate((valid) => {
        if (valid) {
          this.cunForm.archiveSave = this.cunForm.archiveSave.map(form => {
            form.remark = this.cunForm.cunValue;
            return form;
          })
          console.log("this.cunForm", this.cunForm)
          addArchivesList(this.cunForm).then(res => {
            this.$message({ message: '存档成功', type: 'success' })
            this.cunDiaLog = false
            this.cunForm.archiveSave = [];
            this.cunForm.cunValue = '';
          });
          // this.$refs.balanceTable.clearSelection();
        }
      })
    },
    cancelCun() {
      this.cunDiaLog = false;
      this.$refs.balanceTable.clearSelection();
      this.archiveSave = [];
      this.cunForm.cunValue = ''
      this.resetForm('cunForm')
    },
    // dialog确定
    handelClickOk() {
      this.$refs['chaobiaoForm'].validate((valid) => {
        if (valid) {
          if (this.regulationType == 1) {
            addOrUpdateControlSet(this.regulationData).then(res => {
              this.$message.success('操作成功')
              this.dialogVisible = false
              this.resetForm('chaobiaoForm')
            })
          } else {
            let arrData = this.ids.map(item => {
              let obj = {
                ...this.regulationData
              }
              for (const key in item) {
                obj[key] = item[key]
              }
              return obj
            })
            batchControlSet(arrData).then(res => {
              this.$message.success('操作成功')
              this.dialogVisible = false
              this.resetForm('chaobiaoForm')
            })
            console.log(arrData)
          }
        }
      });
    },
    goDataList(row) {
      this.$router.push({
        path: '/data/list',
        query: {
          type: 2,
          plotId: row.plotId,
          buildId: row.buildId,
          unitId: row.unitId,
          houseNumber:row.houseNumber
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
::v-deep .el-table .cell {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

::v-deep .el-dialog .el-form .el-form-item .el-form-item__content {
  width: calc(100% - 120px);
  margin: 0 !important;
}

::v-deep .el-dialog .el-form-item--medium.el-form-item {
  min-width: 33.33%;
  box-sizing: border-box;
  margin-right: 0px;
  display: inline-flex;

  .el-form-item__label {
    width: 250px !important;
  }

}

.dialog-warp {
  max-height: 50vh;
  overflow-y: scroll;
  padding-left: 50px;
}

::v-deep .textarea-warp>.el-form-item {
  display: block;
  margin-bottom: 10px !important;
  padding-right: 30px !important;

  .el-textarea__inner {
    min-height: 120px !important;
    color: #fff;
    background: #444F64;
    border: 0px;
  }
}
</style>
